//
//  TextViewStyle.swift
//  EasyCoding
//
//  Created by Fanxx on 2019/8/12.
//

import UIKit

extension UCSetting where TargetType: UITextView {
    ///文本
    public static func text(_ text:String?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.text = text
        })
    }
    public static func text(_ text:NSAttributedString?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.attributedText = text
        })
    }
    ///字体
    public static func font(_ font:UIFont?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.font = font
        })
    }
    ///系统字体
    public static var systemFont:UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: UIFont.systemFontSize))}
    ///系统字体
    public static func font(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size))}
    ///系统字体
    public static func font(size: CGFloat, weight: UIFont.Weight) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size, weight: weight))}
    ///系统字体
    public static func boldFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.boldSystemFont(ofSize: size))}
    ///系统字体
    public static func italicFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.italicSystemFont(ofSize: size))}
    
    ///文本颜色
    public static func color(_ color:UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.textColor = color
        })
    }
    ///对齐
    public static func align(_ align:NSTextAlignment) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.textAlignment = align
        })
    }
    ///居中对齐
    public static var center: UCSetting<TargetType> {
        return self.align(.center)
    }
    ///左对齐
    public static var left: UCSetting<TargetType> {
        return self.align(.left)
    }
    ///右对齐
    public static var right: UCSetting<TargetType> {
        return self.align(.right)
    }
    ///键盘
    public static func keyboard(_ type:UIKeyboardType) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.keyboardType = type
        })
    }
    ///textContainerInset
    public static func inset(_ inset:UIEdgeInsets) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.textContainerInset = inset
        })
    }
    ///delegate
    public static func delegate(_ delegate:UITextViewDelegate) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.delegate = delegate
        })
    }
    ///selectedRange
    public static func selected(_ range:NSRange) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.selectedRange = range
        })
    }
    ///isEditable
    public static func editable(_ enable:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isEditable = enable
        })
    }
    ///isSelectable
    public static func selectable(_ enable:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isSelectable = enable
        })
    }
    ///dataDetectorTypes
    public static func detect(_ types:UIDataDetectorTypes) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.dataDetectorTypes = types
        })
    }
    ///allowsEditingTextAttributes
    public static func editing(attr enable: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsEditingTextAttributes = enable
        })
    }
    ///typingAttributes
    public static func typing(attr: [NSAttributedString.Key : Any]) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.typingAttributes = attr
        })
    }
    ///linkTextAttributes
    public static func link(attr: [NSAttributedString.Key : Any]) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.linkTextAttributes = attr
        })
    }
    ///自定义键盘 inputView
    public static func input(_ view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.inputView = view
        })
    }
    ///自定义键盘 inputAccessoryView
    public static func input(accessory view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.inputAccessoryView = view
        })
    }
    ///clearsOnInsertion
    public static func clearOnInsert(_ enable: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.clearsOnInsertion = enable
        })
    }
    ///usesStandardTextScaling
    @available(iOS 13.0, *)
    public static func standardScaling(_ enable: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.usesStandardTextScaling = enable
        })
    }
}
